package com.example.peibei.ui.weight;

import android.app.AlertDialog;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.dingtao.common.bean.PayPower;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.exception.ApiException;
import com.example.peibei.OnCallback;
import com.example.peibei.R;
import com.example.peibei.service.presenter.PayPowerPresenter;

import java.util.List;

public class PayStatusDialog extends AlertDialog.Builder {
    AlertDialog dialog;
    String not;
    public PayStatusDialog(Context context) {
        super(context,R.style.FullScreenTheme2);
    }

    public AlertDialog create(int payType,String token,String not, OnCallback onCallback) {
        this.not = not;
       return create(payType,token,onCallback);
    }
    public AlertDialog create(int payType,String token, OnCallback onCallback) {
        dialog = create();
        dialog.show();
        dialog.setContentView(R.layout.dialog_member_pay);

        PayPowerPresenter presenter = new PayPowerPresenter(new PayPowerData());
        presenter.reqeust("Bearer " +token);

        ImageView qianbaoIv=dialog.findViewById(R.id.qianbao_iv);
        ImageView aliPayIv=dialog.findViewById(R.id.ali_pay_iv);
        ImageView wechatIv=dialog.findViewById(R.id.wechat_iv);

        if (payType == 0){
            qianbaoIv.setVisibility(View.VISIBLE);
        }else if (payType == 1){
            wechatIv.setVisibility(View.VISIBLE);
        }else if (payType == 2){
            aliPayIv.setVisibility(View.VISIBLE);
        }

        dialog.findViewById(R.id.layout_qianbao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                onCallback.onCall(0);
            }
        });
        dialog.findViewById(R.id.layout_ali_pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                onCallback.onCall(2);
            }
        });
        dialog.findViewById(R.id.layout_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                onCallback.onCall(1);
            }
        });
        dialog.findViewById(R.id.cancel_tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        return dialog;
    }

    class  PayPowerData implements DataCall<List<PayPower>> {

        @Override
        public void success(List<PayPower> data, Object... args) {
            for (PayPower datum : data) {
                if (datum.getStatus() == 1){
                    if ("0".equals(datum.getPayKey())){
                        if (!"0".equals(not)){
                            dialog.findViewById(R.id.layout_qianbao).setVisibility(View.VISIBLE);
                        }
                    }else if ("1".equals(datum.getPayKey())){
                        if (!"1".equals(not)){
                            dialog.findViewById(R.id.layout_wechat).setVisibility(View.VISIBLE);
                        }
                    }else if ("2".equals(datum.getPayKey())){
                        if (!"2".equals(not)){
                            dialog.findViewById(R.id.layout_ali_pay).setVisibility(View.VISIBLE);
                        }
                    }
                }
            }
        }

        @Override
        public void fail(ApiException data, Object... args) {

        }
    }
}
